using B.S.RbacData.ErrorCode;
using ContentmanagementBaseRead.API.Applications.Commands.Drugstores;
using ContentmanagementBaseRead.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseRead.API.Controllers
{
    /// <summary>
    /// 药店管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DrugstoreController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<DrugstoreController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public DrugstoreController(IMediator mediator, ILogger<DrugstoreController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取药店列表信息
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <returns>药店列表</returns>
        [HttpGet("list")]
        public async Task<ApiPaging<List<DrugstoreListDto>>> GetDrugstoreList([FromQuery] GetDrugstoreListCommand command)
        {
            try
            {
                _logger.LogInformation("接收到获取药店列表信息请求，参数：PageIndex={PageIndex}, PageSize={PageSize}", 
                    command.PageIndex, command.PageSize);

                var result = await _mediator.Send(command);

                _logger.LogInformation("药店列表信息获取请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取药店列表信息请求处理异常");
                return new ApiPaging<List<DrugstoreListDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取药店列表信息请求处理异常：{ex.Message}",
                    data = new List<DrugstoreListDto>(),
                    TotalCount = 0,
                    PageCount = 0
                };
            }
        }
    }
}